package com.bootdo.system.controller;

import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.system.domain.ReportImgDO;
import com.bootdo.system.service.ReportImgService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 质检报告图片信息表
 *
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2021-04-20 11:04:48
 */

@Controller
@RequestMapping("/system/reportImg")
public class ReportImgController {
    @Autowired
    private ReportImgService reportImgService;

    @Value("${bootdo.uploadPath}")
    private String uploadPath;


    @GetMapping()
    @RequiresPermissions("system:reportImg:reportImg")
    String ReportImg() {
        return "system/reportImg/reportImg";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("system:reportImg:reportImg")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<ReportImgDO> reportImgList = reportImgService.list(query);
        int total = reportImgService.count(query);
        PageUtils pageUtils = new PageUtils(reportImgList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("system:reportImg:add")
    String add() {
        return "system/reportImg/add";
    }

    @GetMapping("/edit/{id}")
    String edit(@PathVariable("id") String id, Model model) {
        ReportImgDO reportImg = reportImgService.get(id);
        model.addAttribute("reportImg", reportImg);
        return "system/reportImg/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("system:reportImg:add")
    public R save(@RequestParam("imgs[]") MultipartFile[] files){
        try{
            reportImgService.save(files);
        }catch (Exception e){
            return R.error(e.getMessage());
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    public R update(ReportImgDO reportImg) {
        reportImgService.update(reportImg);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("system:reportImg:remove")
    public R remove(String id) {
        if (reportImgService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("system:reportImg:batchRemove")
    public R remove(@RequestParam("ids[]") String[] ids) {
        reportImgService.batchRemove(ids);
        return R.ok();
    }

    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") String id,Model model){
        ReportImgDO reportImg = reportImgService.get(id);
        model.addAttribute("info", reportImg);
        return "system/reportImg/detail";
    }


}
